package com.wsj.neo4j.simple.config;

import lombok.extern.slf4j.Slf4j;
import org.neo4j.ogm.session.SessionFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.data.neo4j.Neo4jProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
 * @author WangSongJun
 * @date 2019-10-28
 */
@Slf4j
@Configuration
public class DataSourceConf {


    @Primary
    @Bean("neo4jPropertiesPrimary")
    @ConfigurationProperties(prefix = "spring.data.neo4j.primary")
    public Neo4jProperties neo4jPropertiesPrimary() {
        return new Neo4jProperties();
    }

    @Bean("neo4jPropertiesSecondary")
    @ConfigurationProperties(prefix = "spring.data.neo4j.secondary")
    public Neo4jProperties neo4jPropertiesSecondary() {
        return new Neo4jProperties();
    }


    @Primary
    @Bean(name = "primarySessionFactory")
    public SessionFactory primarySessionFactory(Neo4jProperties neo4jProperties) {
        org.neo4j.ogm.config.Configuration configuration = neo4jProperties.createConfiguration();
        return new SessionFactory(configuration, "com.wsj.neo4j.simple.entity");
    }


    @Bean(name = "secondarySessionFactory")
    public SessionFactory secondarySessionFactory(@Qualifier("neo4jPropertiesSecondary") Neo4jProperties neo4jProperties) {
        org.neo4j.ogm.config.Configuration configuration = neo4jProperties.createConfiguration();
        return new SessionFactory(configuration, "com.wsj.neo4j.simple.mds.entity");
    }

}
